import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";

const scene = new THREE.Scene();
const pointLight = new THREE.PointLight(0xffffff, 200);
pointLight.position.set(80, 80, 80);
scene.add(pointLight);

const width = window.innerWidth;
const height = window.innerHeight;

const camera = new THREE.PerspectiveCamera(60, width / height, 1, 10000);
camera.position.set(200, 200, 200);
camera.lookAt(0, 0, 0);

const renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
document.body.append(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);

// 添加管道
const curve = new THREE.CatmullRomCurve3([
	new THREE.Vector3(-100, 20, 90),
	new THREE.Vector3(-40, 80, 100),
	new THREE.Vector3(0, 0, 0),
	new THREE.Vector3(60, -60, 0),
	new THREE.Vector3(100, -40, 80),
	new THREE.Vector3(150, 60, 60),
]);
const geometry = new THREE.TubeGeometry(curve, 100, 5, 30);

const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load("/assets/image/stone.png");
texture.wrapS = THREE.RepeatWrapping;
// texture.wrapT = THREE.RepeatWrapping;
texture.repeat.x = 20;
texture.colorSpace = THREE.SRGBColorSpace;

const material = new THREE.MeshBasicMaterial({
	// color: 0x00ff00,
	map: texture,
	aoMap: texture,
	side: THREE.DoubleSide,
});
const tube = new THREE.Mesh(geometry, material);
scene.add(tube);

const tubePoints = curve.getSpacedPoints(1000);

let i = 0;
function animate() {
	if (i < tubePoints.length - 1) {
		camera.position.copy(tubePoints[i]);
		camera.lookAt(tubePoints[i + 1]);
		i++;
	} else {
		i = 0;
	}

	renderer.render(scene, camera);
	requestAnimationFrame(animate);
}
animate();
